package com.geeklan.smart.ui.device;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.widget.SeekBar;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.base.utils.MLog;
import com.geeklan.smart.R;
import com.geeklan.smart.data.remote.rtc.RtcEngineLocalManager;
import com.geeklan.smart.databinding.ActivityVideoPlayBinding;

import io.agora.rtc2.RtcEngine;

public class VideoPlayActivity extends BaseActivity<ActivityVideoPlayBinding> implements RtcEngineLocalManager.RtcEngineEventListener {
    private String url;
    private RtcEngineLocalManager rtcEngineLocalManager;
    @Override
    protected ActivityVideoPlayBinding initBinding() {
        return ActivityVideoPlayBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        url = getIntent().getStringExtra("url");
        rtcEngineLocalManager = new RtcEngineLocalManager(this,mBinding.frameLayout);
        rtcEngineLocalManager.setRtcEngineEventListener(this);
        rtcEngineLocalManager.open(url);
    }


    @Override
    protected void initView() {
        mBinding.ivBack.setOnClickListener(v -> finish());
        mBinding.progressBar.setMax(100);
        mBinding.progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                rtcEngineLocalManager.getMediaPlayer().seek(rtcEngineLocalManager.getMediaPlayer().getDuration() * seekBar.getProgress() / 100);
            }

        });
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        rtcEngineLocalManager.onDestroy();
    }

    public static void startActivity(Context context, String url) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }

    @Override
    public void onPositionChanged(int progress) {
        MLog.e("progress = " +progress);
        runOnUiThread(() -> mBinding.progressBar.setProgress(progress));

    }
}